package com.bsoft.gol.hbs.message.sendMsg;

import com.bsoft.gol.hbs.utils.CommonUtils;
import ctd.spring.boot.annotation.SsdevReference;
import hcn.base.register.ISmsService;
import hcn.bean.SendMsgDTO;
import hcn.enums.MsgServiceTypeEnum;
import hcn.enums.MsgType;
import hcn.util.BaseResponse;
import hcn.util.ResultCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * @description: 手机短信的消息发送实现
 * @author: zhengcb10708
 * @create: 2020-10-09 11:15
 **/
@Service
public class SMSSendMsgService extends AbstractSendMsgServiceImpl {

    private static Logger logger = LoggerFactory.getLogger(SMSSendMsgService.class);

    @SsdevReference("hcn.smsService")
    private ISmsService smsService;
    /**
     * @Describe: 判断是否进行发消息操作-主要进行配置参数的校验
     * @Param: []
     * @Return: boolean
     * @Author: zhengcb10708
     * @Date: 2020/10/9 13:21
     */
    @Override
    protected boolean isDoSendMsg() {
        return true;
    }

    /**
     * @Describe: 发送消息具体实现
     * @Param: [sendMsgDTO, content]
     * @Return: BaseResponse
     * @Author: zhengcb10708
     * @Date: 2020/10/9 13:21
     */
    @Override
    protected BaseResponse doSendMsg(SendMsgDTO sendMsgDTO, String content) {
        BaseResponse baseResponse = new BaseResponse();
        try{
           /* SmsServiceType smsServiceType = SmsServiceType.getDescription(sendMsgDTO.getSmsserviceType());
            if(CommonUtils.isNull(smsServiceType)){
                baseResponse.setCode(ResultCode.ERROR);
                baseResponse.setMessage(getName()+"传入的SmsserviceType没有对应的短信枚举名称");
                logger.info(getName() + "传入的SmsserviceType没有对应的短信枚举名称");
                return baseResponse;
            }*/
            String mobile = sendMsgDTO.getMobile();
            if (CommonUtils.isNull(mobile)) {
                baseResponse.setCode(ResultCode.ERROR);
                baseResponse.setMessage(getName() + "失败:传入消息参数不全");
                logger.info(getName() + "失败:传入消息参数不全");
                return baseResponse;
            }
            if (CommonUtils.isNotNull(content)&& MsgServiceTypeEnum.SMSSERVICE_ASYNCSENDSMS.getCode().equals(sendMsgDTO.getMsgServiceType())) {
                if (content.contains("$")) {
                    content = content.replaceAll("$", "");
                }
                smsService.asyncSendSms(mobile,content);
                baseResponse.setCode(ResultCode.SUCCESS);
                baseResponse.setMessage("发送短信成功");
            } else if(CommonUtils.isNotNull(content)&& MsgServiceTypeEnum.SMSSERVICE_SYNCSENDSMS.getCode().equals(sendMsgDTO.getMsgServiceType())){
                if (content.contains("$")) {
                    content = content.replaceAll("$", "");
                }
                smsService.syncSendSms(mobile,content);
                baseResponse.setCode(ResultCode.SUCCESS);
                baseResponse.setMessage("发送短信成功");
            } else {
                baseResponse.setCode(ResultCode.ERROR);
                baseResponse.setMessage(getName() + "失败:传入消息参数不全");
                logger.info(getName() + "失败:传入消息参数不全");
            }
        }catch (Exception e){
            baseResponse.setCode(ResultCode.ERROR);
            baseResponse.setMessage(getName() + "方法：doSendMsg出错，原因："+e.getMessage());
            logger.info(getName() + "方法：doSendMsg出错，原因："+e.getMessage());
        }
        return baseResponse;
    }

    @Override
    protected BaseResponse doSendPushMsg(SendMsgDTO sendMsgDTO, String content) {
        return null;
    }

    /**
     * @Describe: 得到具体实现类的名称
     * @Param: []
     * @Return: java.lang.String
     * @Author: zhengcb10708
     * @Date: 2020/10/9 13:22
     */
    @Override
    protected String getName() {
        return MsgType.SMS.getMsgTypeValue();
    }
    /**
     * @Describe: 获取消息类型
     * @Param: []
     * @Return: java.lang.String
     * @Author: zhengcb10708
     * @Date: 2020/10/9 13:22
     */
    @Override
    public String getMsgType() {
        return MsgType.SMS.getMsgTypeKey();
    }
}
